package org.enwey.icarenew.Manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import org.enwey.icarenew.APPUtils.FileUtil;
import org.enwey.icarenew.APPUtils.StringUtil;
import org.enwey.icarenew.Constant;
import org.enwey.icarenew.Model.User;

import it.sauronsoftware.base64.Base64;


public final class SettingsManager {

    private static Context mContext;
    private static SharedPreferences sharedPreferences;
    private static SettingsManager instance;

    private SettingsManager(Context context) {
        mContext = context.getApplicationContext();
        sharedPreferences = mContext.getSharedPreferences("Settings.sp", Context.MODE_PRIVATE);
    }

    public static void init(Context context) {
        if(instance == null || instance.mContext == null) {
            SettingsManager.destroy();
            instance = new SettingsManager(context);
        }
    }

    public static void destroy() {
        if(instance != null) {
            instance.mContext = null;
            instance.sharedPreferences = null;
            instance = null;
        }
    }

    public static SettingsManager getInstance() {
        return instance;
    }

    public static void saveLoginUser(User user) {
        FileUtil.writeSerializableForDefaultPath("User.cache", user);
    }

    // 临时方法
    public static void deleteLoginUser() {
        FileUtil.writeSerializableForDefaultPath("User.cache", null);
    }

    public static User getLoginUser() {
        return (User) FileUtil.readSerializableFromDefaultPath("User.cache");
    }

    public static void saveLoginSession() {
    }

    public static String loadLoginSession() {
        return "";
    }

    public static void setPhone(String phone) {
        sharedPreferences.edit().putString(Constant.SP_KEY_PHONE, phone).commit();
    }

    public static String getPhone() {
        return sharedPreferences.getString(Constant.SP_KEY_PHONE, "");
    }

	public static void setRememberPwd(boolean remember, String pwd) {
		SharedPreferences.Editor editor = sharedPreferences.edit();
		if(remember) {
			editor.putString(Constant.SP_KEY_RM_TOKEN, constructSecretaryString(Base64.encode(pwd)));
		}
		else {
			editor.putString(Constant.SP_KEY_RM_TOKEN, "");
		}
		editor.putBoolean(Constant.SP_KEY_REMEMBER_PWD, remember);
		editor.commit();
	}

	public static boolean isRememberPwd() {
		return sharedPreferences.getBoolean(Constant.SP_KEY_REMEMBER_PWD, false);
	}

	public static String getRememberPwd() {
		boolean isRememberPwd = isRememberPwd();
		String token = sharedPreferences.getString(Constant.SP_KEY_RM_TOKEN, null);
		if(!isRememberPwd || TextUtils.isEmpty(token) || token.length() < 10) {
			return "";
		}
		token = token.substring(5, token.length() - 5);
		return Base64.decode(token);
	}

    public boolean isFirstLaunch() {
        return sharedPreferences.getBoolean(Constant.SP_KEY_FIRST_LAUNCH, true);
    }

    public void setFirstLaunchDone() {
        sharedPreferences.edit().putBoolean(Constant.SP_KEY_FIRST_LAUNCH, false).commit();
    }

    public int getVersionCode(){
        return sharedPreferences.getInt(Constant.SP_KEY_VERSION_CODE, 0);
    }

    public void setVersionCode(int versionCode){
        sharedPreferences.edit().putInt(Constant.SP_KEY_VERSION_CODE, versionCode).commit();
    }

    public static void destroyLogin() {
//        if(Constant.IGNORE_KICK_USER) {
//            return ;
//        }
        FileUtil.deleteSerializableFileForDefaultPath("User.cache");
        saveLoginSession();
    }

    public static boolean isUserLogin() {
        return LoginManager.getInstance(mContext).isLogin();
    }

    private static String constructSecretaryString(String src) {
        String subfix = StringUtil.randomChar(5);
        String prefix =StringUtil.randomChar(5);
        return subfix + src + prefix;
    }

}
